package izsla.sb;

import izsla.entities.Image;
import izsla.entities.News;
import izsla.entities.Participate;
import izsla.entities.SearchCriteria;
import izsla.entities.Trip;
import izsla.entities.TripUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *	A session bean for managing the trips.
 */
@Stateless
public class TripManagement implements TripManagementLocal {
    @PersistenceContext
    EntityManager em;

	/**
	 * Retrives the list according to the given searchCriteria.
	 * @param sc
	 * @return the list of the matching trips
	 */
    public List<Trip> getTrips(SearchCriteria sc) {
        Query q = em.createNamedQuery("Trip.getTrips");

        System.out.println("\nSearching: " + sc.getMinLength() + "\n");

        q.setParameter("minDist", sc.getMinLength());
        q.setParameter("maxDist", sc.getMaxLength());

        q.setParameter("start", sc.getStartTime());
        q.setParameter("end", sc.getEndTime());

        return q.getResultList();
    }

    /**
	 * Find out, if the user is participating in the given trip.
	 * @param user
	 * @param trip
	 * @return
	 */
	public boolean isParticipate(TripUser user, Trip trip) {
		Query q = em.createNamedQuery("Participate.isParticipate");

		q.setParameter("userId", user.getId());
		q.setParameter("tripId", trip.getId());

		Long res = (Long)q.getSingleResult();
		return res > 0;
    }

	/**
	 * Remove the participation entry from the database.
	 * @param part
	 */
	public void deleteParticipation(Participate part) {
		part = em.find(Participate.class, part.getId());
		em.remove(part);
		em.flush();
    }

	/**
	 * Add a new trip to the persistance context.
	 * @param trip
	 */
    public void addTrip(Trip trip) {
		System.out.println("kezdődik");
		em.persist(trip);
		System.out.println("Vége.\n\n");
    }

    /**
	 * Returns the trip, with the given id.
	 * @param id
	 * @return the trip, or null if it is not found.
	 */
	public Trip getTrip(long id) {
		return em.find(Trip.class, id);
    }

	/**
	 * Save the changes made to the trip.
	 * @param trip
	 */
    public void mergeTrip(Trip trip) {
		em.merge(trip);
    }

    public void refreshTrip(Trip trip) {
		em.refresh(trip);
    }

	/**
	 * Delete the news with the given id.
	 * @param id
	 */
    public void deleteNews(long id) {
		News n = em.find(News.class, id);
		Trip trip = n.getTrip();
		em.remove(n);

		if(trip != null) {
			em.refresh(trip);
		}
    }

	/**
	 * Create a new image.
	 * @param trip 
	 * @param user the uploader of the image.
	 * @return
	 */
    public Image createImage(Trip trip, TripUser user) {
		Image img = new Image();
		em.persist(img);

		trip = em.find(Trip.class, trip.getId());
		user = em.find(TripUser.class, user.getId());

		trip.getImages().add(img);
		img.setTrip(trip);
		//em.merge(trip);

		user.getImages().add(img);
		img.setUploader(user);
		//em.merge(user);

		return img;
    }

	/**
	 * Persist the changes of the image.
	 * @param image
	 */
    public void saveImage(Image image) {
		em.merge(image);
		//System.out.println("image saved("+ image.getId() + "). Size=" + image.getImgData().length);
    }

	/**
	 * Get image by id.
	 * @param id
	 * @return the image, or null if it is not found
	 */
    public Image getImage(long id) {
		return em.find(Image.class, id);
    }

	/**
	 * Delete the given trip.
	 * @param trip
	 */
	public void deleteTrip(Trip trip) {
		trip = em.find(Trip.class, trip.getId());
		em.remove(trip);
	}
}
